#include "copycat.h"


//////////////////////////////////////////////////////////////////////////////
bool COutFiles::Prepare( char *arg_dir )
{
	out_dir = arg_dir;
//	fprintf( stderr, "Prepare out files... " );

	char buf[1024];
	sprintf( buf, "rm -rf %s/*; mkdir -p %s",arg_dir, arg_dir );	system( buf );


	sprintf( buf, "%s/origin.parse", arg_dir );
	file_org_parse.OpenFile( buf, "w" );
	if( file_org_parse.IsOpen() == false ){ fprintf( stderr, "file open fail [%s]\n", buf ); return false; }

	sprintf( buf, "%s/origin.S", arg_dir );
	file_org_lex.OpenFile( buf, "w" );
	if( file_org_lex.IsOpen() == false ){ fprintf( stderr, "file open fail [%s]\n", buf ); return false; }

	sprintf( buf, "%s/origin.SP", arg_dir );
	file_org_lexpos.OpenFile( buf, "w" );
	if( file_org_lexpos.IsOpen() == false ){ fprintf( stderr, "file open fail [%s]\n", buf ); return false; }

	sprintf( buf, "%s/reord.parse", arg_dir );
	file_reord_parse.OpenFile( buf, "w" );
	if( file_reord_parse.IsOpen() == false ){ fprintf( stderr, "file open fail [%s]\n", buf ); return false; }

	sprintf( buf, "%s/reord.S", arg_dir );
	file_reord_lex.OpenFile( buf, "w" );
	if( file_reord_lex.IsOpen() == false ){ fprintf( stderr, "file open fail [%s]\n", buf ); return false; }

	sprintf( buf, "%s/reord.SP", arg_dir );
	file_reord_lexpos.OpenFile( buf, "w" );
	if( file_reord_lexpos.IsOpen() == false ){ fprintf( stderr, "file open fail [%s]\n", buf ); return false; }

	sprintf( buf, "%s/reord.rinfo", arg_dir );
	file_reord_rinfo.OpenFile( buf, "w" );
	if( file_reord_rinfo.IsOpen() == false  ){  fprintf( stderr, "file open fail [%s]\n", buf ); return false; }

	sprintf( buf, "%s/stat", arg_dir );
	file_stat.OpenFile( buf, "w" );
	if( file_stat.IsOpen() == false ){ fprintf( stderr, "file open fail [%s]\n", buf ); return false; }

	sprintf( buf, "%s/rules", arg_dir );
	file_rules.OpenFile( buf, "w" );
	if( file_rules.IsOpen() == false ){ fprintf( stderr, "file open fail [%s]\n", buf ); return false; }

	sprintf( buf, "%s/rule.cr", arg_dir );
	file_rule_cr.OpenFile( buf, "w" );
	if( file_rule_cr.IsOpen() == false ){ fprintf( stderr, "file open fail [%s]\n", buf ); return false; }

	sprintf( buf, "%s/rule.rf", arg_dir );
	file_rule_rf.OpenFile( buf, "w" );
	if( file_rule_rf.IsOpen() == false ){ fprintf( stderr, "file open fail [%s]\n", buf ); return false; }

	sprintf( buf, "%s/src-info", arg_dir );
	file_src_info.OpenFile( buf, "w" );
	if( file_src_info.IsOpen() == false ){ fprintf( stderr, "file open fail [%s]\n", buf ); return false; }

//	fprintf( stderr, "[OK]\n" );

	return true;
}


//////////////////////////////////////////////////////////////////////////////
void CCopycat::SaveOrg()
{
	/*
	tree.PrintParse2File( file_org_parse );
	tree.PrintLex2File( file_org_lex );
	tree.PrintLexPos2File( file_org_lexpos );
	*/
}


//////////////////////////////////////////////////////////////////////////////
void CCopycat::SaveReord()
{
	/*
	tree.PrintParse2File( file_reord_parse );
	tree.PrintLex2File( file_reord_lex );
	tree.PrintLexPos2File( file_reord_lexpos );
	tree.PrintReordInfo2File( file_reord_rinfo );
	*/
}


//////////////////////////////////////////////////////////////////////////////
void CCopycat::SaveStat()
{
	/*
	fprintf( file_stat.GetFP(), "%d\n", tree.num_node );	
	*/
}





//////////////////////////////////////////////////////////////////////////////
bool CCopycat::SetNodeSpan( CSent &s, CTree &tree )
{
	/*
	fprintf( stdout, "\n%s\n", s.parse.GetStr() );
	s.align_list.PrintEx( stdout, "  " );
	fprintf( stdout, "\n" );
	s.src_tok_list.PrintEx( stdout, "  " );
	fprintf( stdout, "\n" );
	s.tgt_tok_list.PrintEx( stdout, "  " );
	fprintf( stdout, "\n" );
	*/

	// [-1] [3 5] [-1] [2] 구조
	for( int i=0; i<s.align_list.GetSize(); i++ )	{
		CUString str = s.align_list.GetAt(i);
		
		CUStringList ls; ls.TokenizeByChar( str, " ", false );
		for( int j=0; j<ls.GetSize(); j++ )	{
			int align_tgt_id = atoi( ls.GetAt(j) );
			if( align_tgt_id == -1 ) continue;

			CNode *n = tree.GetNodeByLexID( i );
			if( n == NULL )	{
				fprintf( stdout, "%d !!!! wrong node id\n", i );
				return false;
				continue;
			}

			n->AddAlign( align_tgt_id );

			if( n->span_from == NULL_NUM || n->span_to == NULL_NUM )	{
				n->span_from = align_tgt_id;
				n->span_to = align_tgt_id;
			}
			else {
				if( align_tgt_id  < n->span_from  )		n->span_from = align_tgt_id;
				if( n->span_to     < align_tgt_id )		n->span_to   = align_tgt_id;
			}
		}
	}

	tree.UpdateSpan();
	tree.UpdateTgtAlign();

	return true;
}



























